package com.wfh.activity;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.RouteOverlay;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPlanNode;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.wfh.R;
import com.wfh.service.CoreService;
import com.wfh.util.MyDialog;
import com.wfh.util.Util;

import android.os.Bundle;
import android.util.Log;

public class MapActivity extends CommonActivity implements MKSearchListener{
	private BMapManager mBMapMan = null;
	private MapView mMapView = null;
	public LocationClient mLocationClient = null;
	public BDLocationListener myListener = new MyLocationListener();
	public MyDialog dialog;
	public MKSearch mMKSearch;
	public CoreService service;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		service=HomeActivity.service;
		mBMapMan = new BMapManager(getApplication());
		mBMapMan.init("F3D51C7296D27A27A366FF9632F624EDF8AE852F", null);
		// 注意：请在试用setContentView前初始化BMapManager对象，否则会报错
		super.onCreate(savedInstanceState, R.layout.activity_map);
		
		dialog=dialog.create(this);
		mMapView = (MapView) findViewById(R.id.bmapsView);
		mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
		mLocationClient.registerLocationListener(myListener); // 注册监听函数
		
		mMKSearch=new MKSearch();
		mMKSearch.init(mBMapMan, this);
		
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setAddrType("all");// 返回的定位结果包含地址信息
		option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000
		option.disableCache(true);// 禁止启用缓存定位
		option.setPoiNumber(5);// 最多返回POI个数
		option.setPoiDistance(1000);// poi查询距离
		option.setPoiExtraInfo(true);// //是否需要POI的电话和地址等详细信息
		mLocationClient.setLocOption(option);
		mLocationClient.start();
		dialog.setText("正在定位……");
		dialog.show();
		if (mLocationClient != null && mLocationClient.isStarted()){
			mLocationClient.requestLocation();
		}

	}

	public class MyLocationListener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if(dialog.isShowing()) dialog.hide();
			if (location == null)
				return;
//			Log.i("Latitude",location.getLatitude()+"Longitude"+location.getLongitude());
			
			double latitude = location.getLatitude(); // 纬度
			double longitude = location.getLongitude(); // 经度
			mMapView.setBuiltInZoomControls(true);// 设置启用内置的缩放控件
			MapController mMapController = mMapView.getController();// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
			GeoPoint point = new GeoPoint((int) (latitude * 1E6),
					(int) (longitude * 1E6));// 用给定的经纬度构造一个GeoPoint，单位是微度 (度 *// 1E6)
			mMapController.setCenter(point);// 设置地图中心点
			mMapController.setZoom(15);// 设置地图zoom级别
			
			dialog.setText("正在获取导航……");
			dialog.show();
			
			MKPlanNode start = new MKPlanNode();
			start.pt = point;
			double endlongitude = 0;
			double endlatitude = 0;
			try{
			endlongitude=Double.parseDouble(Util.getShare(MapActivity.this, "longitude"));
			endlatitude=Double.parseDouble(Util.getShare(MapActivity.this, "latitude"));
			}catch(Exception e){
				e.printStackTrace();
			}
			MKPlanNode end = new MKPlanNode();
			end.pt = new GeoPoint((int) (endlatitude * 1E6), (int) ( endlongitude* 1E6));// 设置驾车路线搜索策略，时间优先、费用最少或距离最短
			mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
			mMKSearch.drivingSearch(null, start, null, end);
			
			
			
		}

		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null) {
				return;
			}
			
		}
	}


	protected void onDestroy() {
		mLocationClient.unRegisterLocationListener(myListener);
		mLocationClient.stop();
		mMapView.destroy();
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		if(dialog!=null) dialog.cancel();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		mLocationClient.unRegisterLocationListener(myListener);
		mLocationClient.stop();
		mMapView.onPause();
		if (mBMapMan != null) {
			mBMapMan.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		mLocationClient.registerLocationListener(myListener);
		mLocationClient.start();
		mMapView.onResume();
		if (mBMapMan != null) {
			mBMapMan.start();
		}
		super.onResume();
	}

	
	

	//驾车
	public void onGetDrivingRouteResult(MKDrivingRouteResult result, int iError) {
	        if (result == null) {
	                return;
	        }
	        RouteOverlay routeOverlay = new RouteOverlay(MapActivity.this, mMapView);  // 此处仅展示一个方案作为示例
	        routeOverlay.setData(result.getPlan(0).getRoute(0));
	        mMapView.getOverlays().add(routeOverlay);
	        mMapView.refresh();
	        if(dialog.isShowing()) dialog.hide();
	        
	        mLocationClient.unRegisterLocationListener(myListener);
			mLocationClient.stop();
	}

	@Override
	public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onGetPoiDetailSearchResult(int arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

}
